from utils import *

# player has position, hand, actions, folded
# behavior - tightness, preflopAggro, preflopTight, postflop, slowplay, bluff, dist
# action - Action('RAISE:40'), interpret action.val, action.isType

# game has community, street, playersLeft
def preflopBot(player):
    strength = preflopStrength(player.hand)
    if player.position == 0:
        return (strength <= .8 * player.preflopTight, preflopRaise(strength, 1.4 * player.preflopAggro), callUpTo(strength, 1.1 * player.preflopTight))
    # dealer turn 1
    elif player.position == 1:
        if Game.playersLeft == 1:
            return (strength <= 1.4 * player.preflopTight, preflopRaise(strength, 1.5 * player.preflopAggro), callUpTo(strength, 1.2 * player.preflopTight))
        else:
            return (strength <= 1.25 * player.preflopTight, preflopRaise(strength, 1.2 * player.preflopAggro), callUpTo(strength, player.preflopTight))
    # small blind turn 1
    else:
        if Game.playersLeft == 1:
            return (True, preflopRaise(strength, 1.2 * player.preflopAggro), callUpTo(strength, 1.1 * player.preflopTight))
        else:
            return (True, preflopRaise(strength, player.preflopAggro), callUpTo(strength, player.preflopTight))
    # big blind
    # return (call, raise, callUpTo), raise implies call up to 2 bb more

def preflopStrength(hand):
    # lookup hand
    (c1, c2) = (c.rank for c in hand.cards)
    if c1 > c2:
        (c1, c2) = (c2, c1)
    if hand.suited():
        strength = P[12 - c2][12 - c1]
    else:
        strength = P[12 - c1][12 - c2]        
    return strength

def preflopRaise(strength, aggression):
    if strength <= .04:
        betRange = (6, 15)
    elif strength < .5 * aggression:
        betRange = (5, 12)
    elif strength < 1.2 * aggression:
        betRange = (0, 6)
    else:
        betRange = (0, 0)
    x = random.random()
    return int(betRange[0] * x + betRange[1] * (1 - x))

def callUpTo(strength, tightness):
    if strength <= .05:
        amt = 200
    elif strength <= .10:
        amt = (270 - 2500 * strength)
    elif strength <= .5 * tightness:
        amt = max(30 - 90 * strength, 7)
    elif strength <= .8 * tightness:
        amt = 6
    elif strength <= tightness:
        amt = 4
    else:
        amt = 0
    if Player.me.largestBet >= 6:
        amt += Player.largestBet
    if Game.handId > 40 and Player.me.sawFlopPct < .35:
        return amt + 3
    elif Game.handId > 40 and Player.me.sawFlopPct < .5:
        return amt + 1
    return amt
